const express = require('express');
const router = express.Router();
const dbMod = require('../../db/dbMod');
const bodyParser = require('body-parser');
const data = require('../data');
const MySQL = require('mysql');
let UPDic = data.UPDic;
const loginSQL = require('../../db/loginSQL');


router.post('/login', bodyParser.json(), (req, res) => {
    // 检查用户名是否为空
    if (!req.body["userAccount"]
        || typeof req.body["userAccount"] != "string"
        || !req.body["userAccount"].length) {
        res.json({
            err: 1,
            message: '用户名为空'
        })
    }
    // 检查密码是否为空
    else if (!req.body["password"]
        || typeof req.body["password"] != "string"
        || !req.body["password"].length) {
        res.json({
            err: 1,
            message: '密码为空'
        })
    } else {
        let userAccount = req.body["userAccount"];
        let password = req.body["password"];
        // 下方改数据库操作代码
        // 下方改数据库操作代码
        console.log(userAccount);

        function OxO (data) {
            if (data.length == 0) {
                res.json({
                    err: 1,
                    message: '用户名不存在'
                })
            } else if (data[0].userStatus == 0) {
                res.json({
                    err: 1,
                    message: '用户被封禁'
                })
            } else if (data[0].password != password) {
                res.json({
                    err: 1,
                    message: '密码错误'
                })
            } else {
                res.json({
                    err: 200,
                    message: '登录成功',
                    userID: data[0].userID,
                    userAccount,
                    password,
                    userStatus: data[0].userStatus,
                    userPower: data[0].userPower
                })
            }
            console.log(data)
        }
        loginSQL.login(userAccount, OxO);

    }
})

module.exports = router;